<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wms:item:add']"
          >新增</el-button
        >
      </el-col>
    </el-row>
    <!-- 列表 -->
    <cx-grid
      id="materialManagementGrid"
      ref="table"
      class="grid"
      border
      resizable
      highlight-hover-row
      show-footer
      :oper-btns="false"
      :loading="tableLoading"
      :export-config="{}"
      :edit-config="{ trigger: 'click', mode: 'row' }"
      :columns="columnsData"
      :footer-method="footerMethod"
      :data="tableData"
      @cell-dblclick="onCellDblClick"
    />
    <!-- 添加或修改物料对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="98px"
        class="dialog-form-two"
      >
      <WmsElForm :form="form" :formData="formData"></WmsElForm>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getWmsItem,
  addWmsItem,
  updateWmsItem,
  exportWmsItem,
} from "@/api/wms/item";
import {
  listErpMaterial,
  addErpMaterial,
  delErpMaterial,
} from "@/api/erp/material";
import {
  listWmsSupplier,
} from "@/api/wms/supplier";
import {
  listErpMaterialType,
} from "@/api/erp/materiaType";
import { mapGetters } from "vuex";
// import { itemTypeTreeselect } from "@/api/wms/itemType";
import { columns,formIndexData } from "./mixins";
import { handleTree } from '../../../../utils/ruoyi'
export default {
  name: "WmsItem",
  components: {},
  mixins: [columns,formIndexData],
  dicts: ["wms_item_type"],
  computed: {
    ...mapGetters([
      "warehouseMap",
      "warehouseList",
      "areaList",
      "areaMap",
      "rackList",
      "rackMap",
    ]),
  },
  data() {
    return {
      // 部门树选项
      deptOptions: [],
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 货架表格数据
      rackListByArea: [],
      // 库区表格数据
      areaListByWarehouse: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemNo: null,
        itemName: null,
        itemType: null,
        unit: null,
        rackId: null,
        areaId: null,
        warehouseId: null,
        quantity: null,
        expiryDate: null,
      },
      showMoreCondition: false,
    };
  },
  created() {
    // this.getList();
    // this.getMaterialTypeList();
    // this.getlistWmsSupplier()

    // itemTypeTreeselect().then((response) => {
    //   this.deptOptions = response.data;
    // });
  },
  methods: {
    // 查询供应商
    getlistWmsSupplier(){
      listWmsSupplier({}).then((response) => {
        const { content, totalElements } = response;
        for (let index = 0; index < content.length; index++) {
          content[index].label = content[index].supplierName
        }
        this.formData[3].formColData[1].deptOptions = handleTree(content) || []
      });
    },
    /** 查询物料类型列表 */
    getMaterialTypeList(){
      listErpMaterialType({}).then((response) => {
        for (let index = 0; index < response.length; index++) {
          response[index].label = response[index].typeName
        }
        this.formData[2].formColData[0].deptOptions = handleTree(response) || []
      });
    },
    /** 查询物料列表 */
    getList() {
      this.tableLoading = true;
      const { pageNum, pageSize } = this.queryParams;
      const query = {
        // ...this.queryParams,
        pageNum: undefined,
        pageSize: undefined,
      };
      const pageReq = { page: pageNum - 1, size: pageSize };
      listErpMaterial(query, pageReq).then((response) => {
        // const { content, totalElements } = response;
        this.tableData = response;
        // this.total = totalElements;
        this.tableLoading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        materialNumber: null,
        materialName: null,
        specifications: null,
        brand: null,
        materialTypeId: null,
        producer: null,
        unitMeasurementId: null,
        supplierId: null,
        unitPrice: null,
        barCode: null,
        salesPrice: null,
        minStock: null,
        procurementCycle: null,
        weight: null,
        color: null,
        goodsAllocation: null,
        useTo: null,
        remarks: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物料";
    },
    /** 修改按钮操作 */
    async handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      await getWmsItem(id).then((response) => {
        this.form = response;
        this.open = true;
        this.title = "修改物料";
      });
    },
    submitForm() {
      addErpMaterial(this.form).then((response) => {
        this.$modal.msgSuccess("新增成功");
        this.open = false;
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm1() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.place) {
            this.form.warehouseId = this.form.place[0];
            this.form.areaId = this.form.place[1];
            this.form.rackId = this.form.place[2];
          }
          if (this.form.id != null) {
            updateWmsItem(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWmsItem(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.materialId || this.ids;
      this.$modal
        .confirm('是否确认删除物料编号为"' + ids + '"的数据项？')
        .then(function () {
          return delErpMaterial(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal
        .confirm("是否确认导出所有物料数据项？")
        .then(() => {
          this.exportLoading = true;
          return exportWmsItem(queryParams);
        })
        .then((response) => {
          this.$download.download(response);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
<style scoped lang="scss">
.grid {
  ::v-deep.el-icon-s-operation {
    font-size: 22px;
  }
  ::v-deepa.el-icon-s-operation:hover {
    font-size: 22px;
    color: #3198ff;
  }
  .table-oper {
    .el-button--mini {
      padding: 1px;
    }
  }
  ::v-deep.vxe-table {
    overflow: visible;
  }
  ::v-deep.vxe-table--body-wrapper.body--wrapper {
    overflow-x: visible;
    overflow-y: visible;
  }
  ::v-deep.el-input__inner {
    border: #ffffff solid 1px;
  }
  ::v-deep.el-input-number {
    .el-input__inner {
      border-color: #dcdfe6;
    }
  }
  ::v-deep.el-input__inner:focus {
    border: #409eff solid 1px;
  }
  .btn-group {
    .el-button + .el-button {
      margin-left: 5px;
    }
  }
}
</style>
